﻿using System;
using System.Diagnostics;
using System.Web;
using System.Web.UI;

namespace Awesome.ClientID
{
    public class PageSerializerModule : IHttpModule
    {
        private PageSerializer _serializer;

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(HttpApplicationPreRequestHandlerExecute);
        }

        private void HttpApplicationPreRequestHandlerExecute(object sender, EventArgs e)
        {
            var app = sender as HttpApplication;

            if (app == null)
                return;

            var page = app.Context.Handler as Page;

            if (page == null)
                return;

            page.PreRenderComplete += new EventHandler(PagePreRenderComplete);
        }

        private void PagePreRenderComplete(object sender, EventArgs e)
        {
            new PageSerializer(sender as Page).Serialize();
        }

        public void Dispose()
        {

        }
    }
}
